package com.mengniu.common.social.config.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mengniu.commerce.common.core.constant.CacheConstants;
import com.mengniu.commerce.common.core.utils.SpringUtils;
import com.mengniu.commerce.common.redis.util.RedisUtils;
import com.mengniu.common.social.config.enums.SourceEnum;
import com.mengniu.common.social.config.properties.SocialLoginConfigProperties;
import com.mengniu.common.social.config.properties.SocialProperties;
import com.mengniu.common.social.mengniu98k.AuthMengniu98kRequest;
import com.mengniu.common.social.mengniu98k.AuthMengniuUser;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.*;

/**
 * @author: dgyu-398305246@qq.com
 * @createTime: 2023年12月03 13:40:12
 * @description: 认证授权工具类
 */
public class SocialUtils {

    private static final AuthRedisStateCache STATE_CACHE = SpringUtils.getBean(AuthRedisStateCache.class);

    @SuppressWarnings("unchecked")
    public static AuthResponse<AuthMengniuUser> loginAuth(String source, String code, String state, SocialProperties socialProperties) throws AuthException {
        AuthRequest authRequest = getAuthRequest(source, socialProperties);
        AuthCallback callback = new AuthCallback();
        callback.setCode(code);
        callback.setState(state);
        return authRequest.login(callback);
    }

    public static Boolean logout(String source, String state, SocialProperties socialProperties) throws AuthException {
        AuthMengniu98kRequest authRequest = (AuthMengniu98kRequest) getAuthRequest(source, socialProperties);
        Object cacheObject = RedisUtils.getCacheObject(CacheConstants.SOCIAL_AUTH_CODE_ACCESSTOKEN + state);
        //蒙牛token失效 直接返回成功
        if (ObjectUtil.isNull(cacheObject)) {
            return true;
        }
        String token = (String) cacheObject;
        return authRequest.logout(token);
    }

    public static JSONArray userRoles(String source, String state, SocialProperties socialProperties) throws AuthException {
        AuthMengniu98kRequest authRequest = (AuthMengniu98kRequest) getAuthRequest(source, socialProperties);
        Object cacheObject = RedisUtils.getCacheObject(CacheConstants.SOCIAL_AUTH_CODE_ACCESSTOKEN + state);
        //蒙牛token失效 直接返回成功
        if (ObjectUtil.isNull(cacheObject)) {
            throw new AuthException("用户登录已失效！");
        }
        String token = (String) cacheObject;
        return authRequest.userRoles(token);
    }

    public static JSONArray userRoutes(String source, String state, SocialProperties socialProperties) throws AuthException {
        AuthMengniu98kRequest authRequest = (AuthMengniu98kRequest) getAuthRequest(source, socialProperties);
        Object cacheObject = RedisUtils.getCacheObject(CacheConstants.SOCIAL_AUTH_CODE_ACCESSTOKEN + state);
        //蒙牛token失效 直接返回成功
        if (ObjectUtil.isNull(cacheObject)) {
            throw new AuthException("用户登录已失效！");
        }
        String token = (String) cacheObject;
        return authRequest.userRoutes(token);
    }


    public static JSONArray userPerms(String source, String state, SocialProperties socialProperties) throws AuthException {
        AuthMengniu98kRequest authRequest = (AuthMengniu98kRequest) getAuthRequest(source, socialProperties);
        Object cacheObject = RedisUtils.getCacheObject(CacheConstants.SOCIAL_AUTH_CODE_ACCESSTOKEN + state);
        //蒙牛token失效 直接返回成功
        if (ObjectUtil.isNull(cacheObject)) {
            throw new AuthException("用户登录已失效！");
        }
        String token = (String) cacheObject;
        return authRequest.userPerms(token);
    }

    public static JSONObject userExt(String source, String state, SocialProperties socialProperties) throws AuthException {
        AuthMengniu98kRequest authRequest = (AuthMengniu98kRequest) getAuthRequest(source, socialProperties);
        Object cacheObject = RedisUtils.getCacheObject(CacheConstants.SOCIAL_AUTH_CODE_ACCESSTOKEN + state);
        //蒙牛token失效 直接返回成功
        if (ObjectUtil.isNull(cacheObject)) {
            throw new AuthException("用户登录已失效！");
        }
        String token = (String) cacheObject;
        return authRequest.getAuthMengniuUserExt(token);
    }

    public static AuthRequest getAuthRequest(String source, SocialProperties socialProperties) throws AuthException {
        SocialLoginConfigProperties obj = socialProperties.getType().get(source);
        if (ObjectUtil.isNull(obj)) {
            throw new AuthException("不支持的第三方登录类型");
        }
        return SourceEnum.getSourceEnum(source,obj);
    }
}

